<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\model\FindUser;
use think\Request;
use think\facade\Db;

class Find
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
//        sleep(3);
        $data = \app\model\Find::where("status","=",1)->paginate(10)->toArray();
        $uid = \request()->uid;

        if (!$uid) {
            $uid = isset($_SERVER["HTTP_UID"]) ? $_SERVER["HTTP_UID"] : 0;
        }

        $findUser = FindUser::where('user_id',$uid)->select()->toArray();
        $findUser = array_column($findUser,'find_id',NULL);
        foreach ($data['data'] as $key => $value) {
            if (in_array($value['id'],$findUser)) {
                $data['data'][$key]['is_follow'] = 1;
            } else {
                $data['data'][$key]['is_follow'] = 0;
            }
        }
        return responses($data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //

        $info = \app\model\Find::find($id);
        return responses($info);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 关注
     */
    public function follow(Request  $request){
        $id = \request()->get('id');
        $uid  = \request()->uid;

       # 关注数量 +1
        # 关注的关系表添加数据

        // 启动事务
        Db::startTrans();
        try {
            $data = ['user_id' => $uid,'find_id' => $id];

            $is_follow = FindUser::where($data)->find();
            if (!empty($is_follow)) {
                // 回滚事务
                Db::rollback();
                return responses();
            }
            FindUser::create(['user_id' => $uid,'find_id' => $id]);

            \app\model\Find::where('id', $id)->inc("follow_number")->update();
            // 提交事务
            Db::commit();
            return responses();
        }catch (\Exception $exception) {
            // 回滚事务
            Db::rollback();
            return responses([],2001,"操作失败");
        }


    }


    /**
     * 评论
     */
    public function commentList(){
        $data = [
            [
                'id' => 1,
                'content' => '内容1',
                'user_id' => 1,
                'parent_id' => 0,
                'children' => [
                    'id' => 3,
                    'content' => '内容2',
                    'user_id' => 1,
                    'parent_id' => 0,
                    'children' => [

                    ],
                ],
            ],
            [
                'id' => 2,
                'content' => '内容3',
                'user_id' => 1,
                'parent_id' => 0,
                'children' => [
                    'id' => 4,
                    'content' => '内容3',
                    'user_id' => 1,
                    'parent_id' => 0,
                    'children' => [

                    ],
                ],
            ],

        ];
        return json($data);
    }
}
